﻿Public Class frmDetailedVisit


    ' ***************************************************************
    ' ********************** Private Functions **********************
    ' ***************************************************************

    Private Function GetReadyToAddNewItem() As Boolean
        ' This function will give a new Employee ID Randomly.
        ' Then it get ready to start filling in data. It will
        ' also disable the button so the user cannot try to
        ' save anything without entering the information first.

        Try
            ' Lets get ready to add new items
            Me.VisitdetailsBindingSource.AddNew()
            ' Generate a new Record ID
            Me.RecordIDTextBox.Text = GenerateRandomNumber(100, 9999999).ToString
            ' Takes previous VisitID 
            Me.VisitIDTextBox.Text = SharedVisitId
            ' Disables the add survey button to make sure the user add values
            btnDetailedVisitAddVisit.Enabled = False
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try
    End Function

    ' This turns on/off all input boxes

    Private Function ChangeAllInputBoxesStatus(ByVal NewStatus As Boolean) As Boolean
        Try
            TownComboBox.Enabled = NewStatus
            ModelIDComboBox.Enabled = NewStatus
            FinancingHouseTextBox.Enabled = NewStatus
            RecordIDTextBox.Enabled = NewStatus
            WorkSheetComboBox.Enabled = NewStatus
            ConditonComboBox.Enabled = NewStatus
            DemoComboBox.Enabled = NewStatus
            EquipoTOComboBox.Enabled = NewStatus
            TradeInComboBox.Enabled = NewStatus
            SaleComboBox.Enabled = NewStatus
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try
    End Function

    Private Function AddNewItem() As Boolean
        Try
            Me.Validate()
            Me.Atlantic_toyota_atm_dbDataSet.BeginInit()
            Me.VisitdetailsBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.Atlantic_toyota_atm_dbDataSet)
            Me.Atlantic_toyota_atm_dbDataSet.EndInit()
            Me.Atlantic_toyota_atm_dbDataSet.AcceptChanges()
            MessageBox.Show("Changes Saved.", "Success", MessageBoxButtons.OK, _
                            MessageBoxIcon.Information)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try
    End Function

    Private Function ValidateVisitRecord() As Boolean
        Try
            '******************************************'
            If TownComboBox.Text.Length = 0 Then
                btnDetailedVisitAddVisit.Enabled = False
            Else
                btnDetailedVisitAddVisit.Enabled = True
            End If
            '******************************************'
            If ModelIDComboBox.Text.Length = 0 Then
                btnDetailedVisitAddVisit.Enabled = False
            Else
                btnDetailedVisitAddVisit.Enabled = True
            End If
            '******************************************'
            If FinancingHouseTextBox.Text.Length = 0 Then
                btnDetailedVisitAddVisit.Enabled = False
            Else
                btnDetailedVisitAddVisit.Enabled = True
            End If
            '******************************************'
            'If RecordIDTextBox.Text.Length = 0 Then
            'btnDetailedVisitAddVisit.Enabled = False
            'Else
            'btnDetailedVisitAddVisit.Enabled = True
            'End If
            '******************************************'
            If WorkSheetComboBox.Text.Length = 0 Then
                btnDetailedVisitAddVisit.Enabled = False
            Else
                btnDetailedVisitAddVisit.Enabled = True
            End If
            '******************************************'
            If ConditonComboBox.Text.Length = 0 Then
                btnDetailedVisitAddVisit.Enabled = False
            Else
                btnDetailedVisitAddVisit.Enabled = True
            End If
            '******************************************'
            If DemoComboBox.Text.Length = 0 Then
                btnDetailedVisitAddVisit.Enabled = False
            Else
                btnDetailedVisitAddVisit.Enabled = True
            End If
            '******************************************'
            If EquipoTOComboBox.Text.Length = 0 Then
                btnDetailedVisitAddVisit.Enabled = False
            Else
                btnDetailedVisitAddVisit.Enabled = True
            End If
            '******************************************'
            If TradeInComboBox.Text.Length = 0 Then
                btnDetailedVisitAddVisit.Enabled = False
            Else
                btnDetailedVisitAddVisit.Enabled = True
            End If
            '******************************************'
            If SaleComboBox.Text.Length = 0 Then
                btnDetailedVisitAddVisit.Enabled = False
            Else
                btnDetailedVisitAddVisit.Enabled = True
            End If
            '******************************************'
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try
        Return True
    End Function

    Private Sub frmDetailedVisit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       

        Try
            'btnDetailedVisitAddVisit.Text = "Save"
            'GetReadyToAddNewItem()

            lblHome.Visible = False
            'lblVisit.Visible = False
            lblSurvey.Visible = False
            lblStatistics.Visible = False
            lblInventory.Visible = False
            lblEmployee.Visible = False
            lblClose.Visible = False

            RecordIDTextBox.ReadOnly = True
            VisitIDTextBox.ReadOnly = True

            'TODO: This line of code loads data into the 'Atlantic_toyota_atm_dbDataSet.inventory' table. You can move, or remove it, as needed.
            Me.InventoryTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.inventory)

            lblSave.Text = "Add New"
            btnDetailedVisitAddVisit.Image = GeneralImageSet.Images("Add")
            btnDetailedVisitAddVisit.Enabled = True

            ChangeAllInputBoxesStatus(False)
            'WorkSheetComboBox.Enabled = False
            'ConditonComboBox.Enabled = False
            'ModelIDComboBox.Enabled = False
            'DemoComboBox.Enabled = False
            'EquipoTOComboBox.Enabled = False
            'TradeInComboBox.Enabled = False
            'TownComboBox.Enabled = False
            'SaleComboBox.Enabled = False
            'FinancingHouseTextBox.Enabled = False
            'RecordIDTextBox.Enabled = False
            'VisitIDTextBox.Enabled = False

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub WorkSheetComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WorkSheetComboBox.SelectedIndexChanged
        ValidateVisitRecord()
    End Sub

    Private Sub ConditonComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConditonComboBox.SelectedIndexChanged
        ValidateVisitRecord()
    End Sub

    Private Sub ModelIDComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModelIDComboBox.SelectedIndexChanged
        ValidateVisitRecord()
    End Sub

    Private Sub DemoComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DemoComboBox.SelectedIndexChanged
        ValidateVisitRecord()
    End Sub

    Private Sub EquipoTOComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EquipoTOComboBox.SelectedIndexChanged
        ValidateVisitRecord()
    End Sub

    Private Sub TradeInComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TradeInComboBox.SelectedIndexChanged
        ValidateVisitRecord()
    End Sub

    Private Sub TownComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TownComboBox.SelectedIndexChanged
        ValidateVisitRecord()
    End Sub

    Private Sub SaleComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaleComboBox.SelectedIndexChanged
        ValidateVisitRecord()
    End Sub

    Private Sub FinancingHouseTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FinancingHouseTextBox.TextChanged
        ValidateVisitRecord()
    End Sub

    Private Sub btnDetailedVisitAddVisit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetailedVisitAddVisit.Click
        Try
            If lblSave.Text = "Add New" Then

                ' If I am here is because I just came to the form
                ' or because I just saved a value and is starting
                ' up again. So we will enable the text boxes and 
                ' change the button label to Save to make it 
                ' consistent.
                btnDetailedVisitAddVisit.Image = GeneralImageSet.Images("Save")
                lblSave.Text = "Save"

                WorkSheetComboBox.Enabled = True
                ConditonComboBox.Enabled = True
                ModelIDComboBox.Enabled = True
                DemoComboBox.Enabled = True
                EquipoTOComboBox.Enabled = True
                TradeInComboBox.Enabled = True
                TownComboBox.Enabled = True
                SaleComboBox.Enabled = True
                FinancingHouseTextBox.Enabled = True
                RecordIDTextBox.Enabled = True
                VisitIDTextBox.Enabled = True

                GetReadyToAddNewItem()

            ElseIf lblSave.Text = "Save" Then

                ' If I am here is because Im about to add a value 
                ' So we will disable the text boxes and change 
                ' the button label to Add New to make it consistent.

                btnDetailedVisitAddVisit.Image = GeneralImageSet.Images("Add")
                lblSave.Text = "Add New"

                WorkSheetComboBox.Enabled = False
                ConditonComboBox.Enabled = False
                ModelIDComboBox.Enabled = False
                DemoComboBox.Enabled = False
                EquipoTOComboBox.Enabled = False
                TradeInComboBox.Enabled = False
                TownComboBox.Enabled = False
                SaleComboBox.Enabled = False
                FinancingHouseTextBox.Enabled = False
                RecordIDTextBox.Enabled = False
                VisitIDTextBox.Enabled = False

                AddNewItem()

                ' Fix Bug#1
                SwitchForm(My.Forms.frmQuickVisit)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnDetailedVisitClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        CloseAllForms()
    End Sub

    Private Sub btnClose_Visit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Dim Confirmation As MsgBoxResult
        Confirmation = MessageBox.Show("Unsaved changes will be lost. Are you sure you want to exit?", _
                                       "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If Confirmation = MsgBoxResult.Yes Then
            CloseAllForms()
        End If
    End Sub

    Private Sub btnHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHome.Click
        SwitchForm(My.Forms.frmMain)
    End Sub

    Private Sub btnSurvey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSurvey.Click
        SwitchForm(My.Forms.frmSurvey)
    End Sub

    Private Sub btnStatistics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStatistics.Click
        SwitchForm(My.Forms.frmStatistics)
    End Sub

    Private Sub btnInventory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInventory.Click
        SwitchForm(My.Forms.frmViewInventory)
    End Sub

    Private Sub btnEmployee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmployee.Click
        SwitchForm(My.Forms.frmViewEmployee)
    End Sub

    '************************************************
    ' Show the label for the items on the main menu *
    '************************************************

    Private Sub btnHome_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnHome.MouseHover
        lblHome.Visible = True
    End Sub

    'Private Sub btnVisit_MouseOver(ByVal sender As System.Object, _
    '              ByVal e As System.EventArgs) Handles btnVisit.MouseHover
    '    lblVisit.Visible = True
    'End Sub

    Private Sub btnSurvey_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnSurvey.MouseHover
        lblSurvey.Visible = True
    End Sub

    Private Sub btnStatistics_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnStatistics.MouseHover
        lblStatistics.Visible = True
    End Sub

    Private Sub btnInventory_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnInventory.MouseHover
        lblInventory.Visible = True
    End Sub

    Private Sub btnEmployee_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnEmployee.MouseHover
        lblEmployee.Visible = True
    End Sub

    Private Sub btnClose_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnClose.MouseHover
        lblClose.Visible = True
    End Sub


    '**************************************
    ' Hides the labels from the main menu *
    '**************************************


    Private Sub btnHome_MouseLeave(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles btnHome.MouseLeave
        lblHome.Visible = False
    End Sub

    'Private Sub btnVisit_MouseLeave(ByVal sender As System.Object, _
    '              ByVal e As System.EventArgs) Handles btnVisit.MouseLeave
    '    lblVisit.Visible = False
    'End Sub

    Private Sub btnSurvey_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnSurvey.MouseLeave
        lblSurvey.Visible = False
    End Sub

    Private Sub btnStatistics_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnStatistics.MouseLeave
        lblStatistics.Visible = False
    End Sub

    Private Sub btnInventory_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnInventory.MouseLeave
        lblInventory.Visible = False
    End Sub

    Private Sub btnEmployee_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnEmployee.MouseLeave
        lblEmployee.Visible = False
    End Sub

    Private Sub btnClose_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnClose.MouseLeave
        lblClose.Visible = False
    End Sub

End Class